//
//  PhotoPreviewVC.swift
//  HQPPhotoPreview
//
//  Created by admin on 2021/9/2.
//

import UIKit
import SnapKit
import SDWebImage
import DDPhotoBrowser

public class PhotoPreviewVC:NSObject {
   
    public class func showPhotoView(_ currentVC:UIViewController,imageUrlArray:[String]?=[], imageViewArray:[UIImageView]?=[],image:UIImage?=nil,startIndex:Int?=0){
        
        var imageDataArray:[DDPhotoItem] = [DDPhotoItem]()
        guard let urlArray = imageUrlArray else {
            return
        }
        if urlArray.count == 0 {
            let item = DDPhotoItem(sourceView: imageViewArray?[0], imageUrl: nil, thumbImage: nil, thumbImageUrl: nil)
            if let curImage = image {
                item?.image = curImage
            }
            imageDataArray.append(item ?? DDPhotoItem())
        }else{
            for (index,urlStr) in urlArray.enumerated() {
                var imageView:UIImageView = UIImageView()
                if (imageViewArray?.count ?? 0)>index {
                    imageView = imageViewArray![index]
                }
                let item = DDPhotoItem(sourceView: imageView, imageUrl: URL(string: urlStr), thumbImage: nil, thumbImageUrl: nil)
                if index == startIndex {
                    item?.firstShowAnimation = true
                }
                imageDataArray.append(item ?? DDPhotoItem())
            }
        }
        
        let downloadEngine = DDPhotoSDImageDownloadEngine.init()
//        图片选择器展示
        let b = DDPhotoBrowser.init(photoItems: imageDataArray, currentIndex: UInt(startIndex ?? 0), getImageViewClass: DDSystemImageView.self, downloadEngine: downloadEngine)
        b?.pageIndicateStyle = .pageLabel
        b?.longPressGestureClickedBlock = { photoBrowser, index, item, imageData in
            print("长按手势回调")
        }
        
        print("sssssssssssssssssssss")
       
        b?.show(fromVC: currentVC)
    }
}
